package com.example.mydy.model;

import java.util.Date;

/**
 * 用户活跃度数据模型
 */
public class ActivityData {
    // 用户ID
    private int userId;
    
    // 用户名
    private String username;
    
    // 用户昵称
    private String nickname;
    
    // 日活跃度
    private int dailyActivity;
    
    // 周活跃度
    private int weeklyActivity;
    
    // 月活跃度
    private int monthlyActivity;
    
    // 连续登录天数
    private int consecutiveLogins;
    
    // 累计提交任务数
    private int totalTasksSubmitted;
    
    // 累计完成悬赏数
    private int totalRewardsCompleted;
    
    // 活跃度等级 (1-5)
    private int activityLevel;
    
    // 最后更新时间
    private Date lastUpdated;
    
    // 今日是否已签到
    private boolean checkedInToday;
    
    // 今日活跃度点数（用于计算每日活跃值）
    private int todayActivityPoints;
    
    // 累计活跃时长(分钟)
    private long totalActiveTime;
    
    public ActivityData() {
        // 默认构造函数
        lastUpdated = new Date();
    }
    
    public ActivityData(int userId, String username) {
        this.userId = userId;
        this.username = username;
        this.nickname = username; // 默认昵称与用户名相同
        this.dailyActivity = 0;
        this.weeklyActivity = 0;
        this.monthlyActivity = 0;
        this.consecutiveLogins = 0;
        this.totalTasksSubmitted = 0;
        this.totalRewardsCompleted = 0;
        this.activityLevel = 1;
        this.lastUpdated = new Date();
        this.checkedInToday = false;
        this.todayActivityPoints = 0;
        this.totalActiveTime = 0;
    }
    
    // 增加日活跃度
    public void addDailyActivity(int points) {
        this.dailyActivity += points;
        this.todayActivityPoints += points;
        this.weeklyActivity += points;
        this.monthlyActivity += points;
        this.lastUpdated = new Date();
        updateActivityLevel();
    }
    
    // 登录时调用
    public void login() {
        // 检查是否是新的一天
        if (!isCheckedInToday()) {
            consecutiveLogins++;
            checkedInToday = true;
            addDailyActivity(10); // 登录奖励10点活跃度
        }
        this.lastUpdated = new Date();
    }
    
    // 提交任务时调用
    public void submitTask() {
        totalTasksSubmitted++;
        addDailyActivity(20); // 提交任务奖励20点活跃度
    }
    
    // 完成悬赏时调用
    public void completeReward() {
        totalRewardsCompleted++;
        addDailyActivity(30); // 完成悬赏奖励30点活跃度
    }
    
    // 更新活跃度等级
    private void updateActivityLevel() {
        if (monthlyActivity >= 1000) {
            activityLevel = 5; // 钻石会员
        } else if (monthlyActivity >= 500) {
            activityLevel = 4; // 铂金会员
        } else if (monthlyActivity >= 200) {
            activityLevel = 3; // 金牌会员
        } else if (monthlyActivity >= 50) {
            activityLevel = 2; // 银牌会员
        } else {
            activityLevel = 1; // 铜牌会员
        }
    }
    
    // 重置日活跃度（每日凌晨调用）
    public void resetDailyActivity() {
        this.dailyActivity = 0;
        this.todayActivityPoints = 0;
        this.checkedInToday = false;
    }
    
    // 重置周活跃度（每周一凌晨调用）
    public void resetWeeklyActivity() {
        this.weeklyActivity = 0;
    }
    
    // 重置月活跃度（每月1日凌晨调用）
    public void resetMonthlyActivity() {
        this.monthlyActivity = 0;
    }
    
    // 检查今日是否已签到
    public boolean isCheckedInToday() {
        return checkedInToday;
    }
    
    // Getters and Setters
    public int getUserId() {
        return userId;
    }
    
    public void setUserId(int userId) {
        this.userId = userId;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    /**
     * 获取用户昵称
     */
    public String getNickname() {
        return nickname != null ? nickname : username;
    }
    
    /**
     * 设置用户昵称
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    
    public int getDailyActivity() {
        return dailyActivity;
    }
    
    public void setDailyActivity(int dailyActivity) {
        this.dailyActivity = dailyActivity;
    }
    
    public int getWeeklyActivity() {
        return weeklyActivity;
    }
    
    public void setWeeklyActivity(int weeklyActivity) {
        this.weeklyActivity = weeklyActivity;
    }
    
    public int getMonthlyActivity() {
        return monthlyActivity;
    }
    
    public void setMonthlyActivity(int monthlyActivity) {
        this.monthlyActivity = monthlyActivity;
    }
    
    public int getConsecutiveLogins() {
        return consecutiveLogins;
    }
    
    public void setConsecutiveLogins(int consecutiveLogins) {
        this.consecutiveLogins = consecutiveLogins;
    }
    
    public int getTotalTasksSubmitted() {
        return totalTasksSubmitted;
    }
    
    public void setTotalTasksSubmitted(int totalTasksSubmitted) {
        this.totalTasksSubmitted = totalTasksSubmitted;
    }
    
    public int getTotalRewardsCompleted() {
        return totalRewardsCompleted;
    }
    
    public void setTotalRewardsCompleted(int totalRewardsCompleted) {
        this.totalRewardsCompleted = totalRewardsCompleted;
    }
    
    public int getActivityLevel() {
        return activityLevel;
    }
    
    public void setActivityLevel(int activityLevel) {
        this.activityLevel = activityLevel;
    }
    
    public Date getLastUpdated() {
        return lastUpdated;
    }
    
    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }
    
    public void setCheckedInToday(boolean checkedInToday) {
        this.checkedInToday = checkedInToday;
    }
    
    public int getTodayActivityPoints() {
        return todayActivityPoints;
    }
    
    public void setTodayActivityPoints(int todayActivityPoints) {
        this.todayActivityPoints = todayActivityPoints;
    }
    
    public long getTotalActiveTime() {
        return totalActiveTime;
    }
    
    public void setTotalActiveTime(long totalActiveTime) {
        this.totalActiveTime = totalActiveTime;
    }
    
    public void addActiveTime(long minutes) {
        this.totalActiveTime += minutes;
    }
    
    // 获取活跃度等级名称
    public String getActivityLevelName() {
        switch (activityLevel) {
            case 5:
                return "钻石会员";
            case 4:
                return "铂金会员";
            case 3:
                return "金牌会员";
            case 2:
                return "银牌会员";
            default:
                return "铜牌会员";
        }
    }
    
    // 获取下一等级所需活跃度
    public int getNextLevelRequiredActivity() {
        switch (activityLevel) {
            case 1:
                return 50 - monthlyActivity; // 到达银牌需要50点
            case 2:
                return 200 - monthlyActivity; // 到达金牌需要200点
            case 3:
                return 500 - monthlyActivity; // 到达铂金需要500点
            case 4:
                return 1000 - monthlyActivity; // 到达钻石需要1000点
            default:
                return 0; // 已是最高等级
        }
    }
} 